﻿/*
*┌────────────────────────────────────────────────┐
*│　描    述：DapperDBHelper                                                   
*│　作    者： (Jxw)                                             
*│　版    本：1.0     
*│　功能描述：                                          
*│　创建时间：2021/4/20 14:01:07                        
*└────────────────────────────────────────────────┘
*/

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Utility
{
    public class DapperDBHelper
    {
        private static bool ConnectionStringsKeyExists(string strKey)
        {
            System.Configuration.ConnectionStringSettingsCollection conns = ConfigurationManager.ConnectionStrings;
            for (int i = 0; i < conns.Count; i++)
            {
                if (conns[i].Name == strKey)
                {
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// 数据库连接字符串
        /// </summary>
        private static string ConnectionString
        {
            get
            {
                string conn = string.Empty;

                if (ConnectionStringsKeyExists("ConnString"))
                {
                    conn = ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString;
                }
                return conn;
            }
        }



        /// <summary>
        /// 数据库连接
        /// </summary>
        /// <returns></returns>
        public static IDbConnection InitConn()
        {
            SqlConnection conn = new SqlConnection(ConnectionString);
            conn.Open();
            return conn;
        }



        /// <summary>
        /// 数据库连接字符串
        /// </summary>
        private static string MainConnectionString
        {
            get
            {
                string conn = string.Empty;

                if (ConnectionStringsKeyExists("MainConnString"))
                {
                    conn = ConfigurationManager.ConnectionStrings["MainConnString"].ConnectionString;
                }
                return conn;
            }
        }

        /// <summary>
        /// 数据库连接
        /// </summary>
        /// <returns></returns>
        public static IDbConnection MainInitConn()
        {
            SqlConnection conn = new SqlConnection(MainConnectionString);
            conn.Open();
            return conn;
        }


        /// <summary>
        /// 数据库连接字符串
        /// </summary>
        private static string QueryMainConnString
        {
            get
            {
                string conn = string.Empty;

                if (ConnectionStringsKeyExists("QueryMainConnString"))
                {
                    conn = ConfigurationManager.ConnectionStrings["QueryMainConnString"].ConnectionString;
                }
                return conn;
            }
        }

        /// <summary>
        /// 查询数据库连接
        /// </summary>
        /// <returns></returns>
        public static IDbConnection QueryMainInitConn()
        {
            SqlConnection conn = new SqlConnection(QueryMainConnString);
            conn.Open();
            return conn;
        }
    }
}
